using System.Drawing;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.BandedGrid;

namespace Demo
{
    internal static class GridColumnFactory
    {
        internal static void ConfigureCommonColumns(BandedGridView view)
        {
            view.BeginUpdate();
            try
            {
                view.Bands.Clear();
                view.Columns.Clear();

                view.OptionsView.ShowBands = false;
                view.OptionsView.ShowColumnHeaders = true;
                view.OptionsView.ShowAutoFilterRow = true;
                view.OptionsView.ShowFooter = false;
                view.OptionsView.ColumnAutoWidth = false;
                view.OptionsView.ShowGroupPanel = false;
                view.IndicatorWidth = 60;
                view.BandPanelRowHeight = 36;
                view.ColumnPanelRowHeight = 36;

                view.Appearance.HeaderPanel.BackColor = Color.White;
                view.Appearance.HeaderPanel.ForeColor = Color.Black;
                view.Appearance.HeaderPanel.Font = new Font("微软雅黑", 9F, FontStyle.Bold);
                view.Appearance.HeaderPanel.Options.UseBackColor = true;
                view.Appearance.HeaderPanel.Options.UseForeColor = true;
                view.Appearance.HeaderPanel.Options.UseFont = true;
                view.Appearance.HeaderPanel.Options.UseTextOptions = true;
                view.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;
                view.Appearance.HeaderPanel.TextOptions.VAlignment = VertAlignment.Center;

                view.Appearance.Row.Options.UseTextOptions = true;
                view.Appearance.Row.TextOptions.HAlignment = HorzAlignment.Center;
                view.Appearance.Row.TextOptions.VAlignment = VertAlignment.Center;

                var dataBand = new GridBand();
                view.Bands.Add(dataBand);

                void AddColumn(string fieldName, string caption, bool visible = true, string? formatString = null, FormatType formatType = FormatType.None, int width = 120)
                {
                    var column = new BandedGridColumn
                    {
                        FieldName = fieldName,
                        Caption = caption,
                        Visible = visible,
                        Width = width
                    };

                    column.OptionsColumn.AllowEdit = false;
                    column.OptionsColumn.AllowMerge = DefaultBoolean.False;
                    column.OptionsColumn.ReadOnly = true;
                    column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                    column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;

                    column.AppearanceCell.Options.UseTextOptions = true;
                    column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                    column.AppearanceCell.TextOptions.VAlignment = VertAlignment.Center;

                    if (!string.IsNullOrEmpty(formatString))
                    {
                        column.DisplayFormat.FormatString = formatString;
                        column.DisplayFormat.FormatType = formatType;
                    }

                    view.Columns.Add(column);
                    dataBand.Columns.Add(column);
                }

                AddColumn("EntryID", "EntryID", visible: false);
                AddColumn("FType", "FType", visible: false);
                AddColumn("ParameterNumber", "参数编号");
                AddColumn("MainTesterName", "主试名字");
                AddColumn("ChildName", "儿童名字");
                AddColumn("TestDate", "测试日期", formatString: "yyyy-MM-dd", formatType: FormatType.DateTime);
                AddColumn("BirthDate", "出生日期", formatString: "yyyy-MM-dd", formatType: FormatType.DateTime);
                AddColumn("Grade", "所在年级");
                AddColumn("ParentEducation", "家长教育程度");
                AddColumn("PhoneNumber", "电话号码");
                AddColumn("VideoGame", "电子游戏");
                AddColumn("Address", "联系地址", width: 160);
                AddColumn("Remarks", "备注资料", width: 160);
                AddColumn("ChildGender", "儿童性别", width: 100);
                AddColumn("HandPreference", "用手习惯", width: 110);

                AddColumn("Blocks", "积木", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("Comprehension", "常识", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("MatrixReasoning", "矩阵推理", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("BugSearch", "找虫", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("PictureMemory", "图片记忆", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("Similarities", "类同", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("PictureConcepts", "图书概念", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("Cancellation", "画消", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("AnimalHome", "动物家园", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("Puzzle", "拼图", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("AnimalCoding", "动物译码", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("Understanding", "理解", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("PictureRecognition", "指认图片", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("PictureNaming", "图片命名", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("RandomCancellation", "随机画消", formatString: "0.##", formatType: FormatType.Numeric);
                AddColumn("OrderedCancellation", "有序画消", formatString: "0.##", formatType: FormatType.Numeric);

                AddColumn("ConfidenceLevel", "信赖水平", width: 120);
                AddColumn("StatisticalSignificance", "统计显著性水准", width: 150);
                AddColumn("ComparisonBasis", "比较基础", width: 120);
                AddColumn("CalculationMethod", "计算方法1", width: 120);
                AddColumn("CalculationMethod2", "计算方法2", width: 120);
                AddColumn("FilePath", "文件路径", width: 200);
            }
            finally
            {
                view.EndUpdate();
            }
        }
    }
}
